<template>
  <div>
    <div style="margin-bottom: 20px;font-size: medium;font-weight: 900;">单位信息</div>
    <!--    输入框区域-->
    <div class="search">
      <el-form label-width="90px" :inline="true"  :model="form" ref="formRef">
        <el-form-item label="参与单位" prop="description">
          <el-select v-model="form.employeeId" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in headerData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="description">
          <el-select v-model="form.employeeId" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in headerData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申报周期" prop="year">
          <el-date-picker type="date" placeholder="yyyy/mm/日" v-model="form.year" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
    </div>

    <div class="operation">
      <div style="font-size: medium;font-weight: 900;">工程明细
        <el-button plain @click="handleAdd" style="margin-left:20px;font-size: 12px; line-height: 8px; color: #409EFF;">新增</el-button>
        <el-button plain @click="delBatch" style="margin-left:20px;font-size: 12px; line-height: 8px; color: #409EFF;">删除</el-button>
      </div>
    </div>

    <!--表单区域-->
    <div class="table">
      <el-table :data="tableData" stripe border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="工作内容" width="100" align="center" sortable></el-table-column>
        <el-table-column prop="name" label="计量单位"></el-table-column>
        <el-table-column prop="description" label="工程量"></el-table-column>
        <el-table-column prop="employeeName" label="本期完成工程量"></el-table-column>
        <el-table-column prop="employeeName" label="剩余工程量"></el-table-column>

        <el-table-column label="操作" width="150" align="center">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click=del(scope.row.id)></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

<!--图片显示的区域-->
    <div @mouseover="showDeleteButton = true" @mouseleave="showDeleteButton = false">
      <!-- 显示图片 -->
        <el-form label-width="80px" :inline="true" style="margin-top: 20px;">
          <el-form-item label="上传凭证">
            <el-image style="width: 100px; height: 100px" :src="url" :preview-src-list="srcList" alt="Image">
            </el-image>
            <el-button v-if="showDeleteButton" @click="deleteImage" icon="el-icon-delete"></el-button>
          </el-form-item>
        </el-form>
        <div class="label">支持文件格式: doc,docx,pdf,jpg,png,xlsx,xls,rar,zip</div>
    </div>


    <!--    上传照片的弹框-->
    <el-dialog title="上传图片" :visible.sync="uploadFromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <!-- 上传 -->
      <el-form label-width="80px" :inline="true" style="margin-top: 20px;">
        <el-form-item label="上传凭证">
          <el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/"
                     :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" list-type="picture">
            <el-button size="medium" icon="el-icon-upload" style="color: #409EFF"></el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="label">支持文件格式: doc,docx,pdf,jpg,png,xlsx,xls,rar,zip</div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="uploadFromVisible = false">取 消</el-button>
        <el-button type="primary" @click="upload">确 定</el-button>
      </div>
    </el-dialog>


    <!--    对话框区域-->
    <el-dialog title="工程明细" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form label-width="100px" style="padding-right: 50px" :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="description" label="工作内容">
          <el-select v-model="form.employeeId" placeholder="请选择主管" style="width: 100%">
            <el-option v-for="item in headerData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="name" label="计量单位">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="工程量">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="本期完成工程量">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="剩余工程量">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 底部区域 -->
    <span slot="footer">
                <el-button type="primary" style="margin-top: 80px;margin-left:400px" @click="save1">保 存</el-button>
      <router-link to="/salary"><el-button style="margin-left:30px">取 消</el-button></router-link>
            </span>
  </div>
</template>

<script>
export default {
  name: "AddProject",
  data() {
    return {
      tableData: [],  // 所有的数据
      fileList:[],
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      name: null,
      fromVisible: false,
      uploadFromVisible:false,
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'},
        ],
        employeeId: [
          {required: true, message: '请选择部门主管', trigger: 'blur'},
        ]
      },
      ids: [],
      headerData: [],
      url: 'https://imgcdn.lelewen.cn/wenda/220503/3adc7bf84d2e98b53694ce229e9c4266.png',
      srcList: [
        'https://imgcdn.lelewen.cn/wenda/220503/3adc7bf84d2e98b53694ce229e9c4266.png',
      ],
      showDeleteButton: false,
    }
  },
  created() {
    this.load(1)
    this.loadHeaderData()
  },
  methods: {
    loadHeaderData() {
      this.$request.get('/employee/selectHeaders').then(res => {
        if (res.code === '200') {
          this.headerData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/department/update' : '/department/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    save1() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/department/update' : '/department/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.$router.push('/salary')
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/department/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)   //  [1,2]
    },
    delBatch() {   // 批量删除
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确定删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/department/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/department/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    deleteImage() {
      this.$confirm("是否删除该照片?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
            this.url = "";
            this.srcList="";
            this.uploadFromVisible="true";
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
    },
    upload(){
      this.url= 'https://imgcdn.lelewen.cn/wenda/220503/3adc7bf84d2e98b53694ce229e9c4266.png',
          this.srcList= [
        'https://imgcdn.lelewen.cn/wenda/220503/3adc7bf84d2e98b53694ce229e9c4266.png',
      ],
          this.uploadFromVisible="false"
    },
  }
}
</script>

<style scoped>

.label {
  color: #409EFF;
  font-size: 10px;
}
</style>
